package one.stand.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import one.stand.model.CompanyModel;
import one.stand.model.ResultModel;
import one.stand.model.essay.EssayShareDetailModel;
import one.stand.model.essay.EssayStaffDataModel;
import one.stand.request.essay.EssayShareDetailRequest;
import one.stand.request.essay.EssayStaffDataFrom;
import one.stand.request.essay.EssayStaffDataRequest;
import one.stand.response.essay.EssayShareDetailResponse;
import one.stand.response.essay.EssayStaffDataResponse;
import one.stand.service.BaseService;
import one.stand.service.IDynamicService;
import one.stand.service.IEssayStaffDataService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 获客文章员工数据 服务实现类
 * </p>
 *
 * @author luoyang
 * @since 2020-05-07
 */
@Service
@Transactional
public class EssayStaffDataServiceImpl  extends BaseService implements IEssayStaffDataService {

    @Autowired
    IDynamicService iDynamicService;

    @Override
    public ResultModel<PageInfo<EssayStaffDataResponse>> queryStaffData(EssayStaffDataRequest request) {

        if(request.getCompanyId() == null){
            request.setCompanyId(request.getCId());
        }
        request.setUserId(request.getId());
        if(request.getSort() == null){
            request.setSort(1);
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        log.info("查询员工分享数据详情: {}", JSON.toJSONString(request));

        List<EssayStaffDataResponse> list =  this.essayStaffDataMapper.queryStaffData(request);
        PageInfo<EssayStaffDataResponse> pageInfo = new PageInfo(list);
        return ResultModel.success(pageInfo);
    }

    @Override
    public ResultModel<PageInfo<EssayShareDetailResponse>> queryStaffShareDetail(EssayShareDetailRequest request) {
        //默认登录用户
        if(request.getUserId() == null){
            request.setUserId(request.getId());
        }
        request.setCompanyId(request.getCId());

        EssayShareDetailModel  model = new EssayShareDetailModel();
        BeanUtils.copyProperties(request,model);

        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        log.info("查询员工分享数据详情: {}", JSON.toJSONString(model));

        List<EssayShareDetailResponse> list =  this.essayStaffDataMapper.queryStaffShareDetail(model);

        PageInfo<EssayShareDetailResponse> pageInfo = new PageInfo(list);
        return ResultModel.success(pageInfo);
    }

    /**
     * 添加员工获客数据
     * @param request
     * @return
     */
    @Override
    public ResultModel<Boolean> addStaffData(EssayStaffDataFrom request) {
        if(request.getCompanyId() == null){
            request.setCompanyId(request.getCId());
        }
        //按 员工 公司 文章查询 是否存在 存在则更新 不存在则添加
        EssayStaffDataModel model = this.essayStaffDataMapper.queryData(request);
        if(model == null){
            model = new EssayStaffDataModel();
            BeanUtils.copyProperties(request,model);
            model = countModel(model,request);
            if(this.essayStaffDataMapper.insert(model) > 0){
                return ResultModel.success();
            }
            return ResultModel.fail();
        }else if(request.getType() != null){
            model = countModel(model,request);
            if(this.essayStaffDataMapper.update(model) > 0){
                return ResultModel.success();
            }
        }
        return ResultModel.fail();
    }

    private EssayStaffDataModel countModel( EssayStaffDataModel model,EssayStaffDataFrom request){
//            更新类型:1 分享文章数量 2 分享次数 3获客数
        switch (request.getType()){
            case 1:
                model.setShareEssayNum(model.getShareEssayNum() +1);
                break;
            case 2:
                model.setShareNum(model.getShareNum() +1);
                //更新动态的分享记录
                iDynamicService.updateShareNum(request.getDynamicId().intValue());
                break;
            case 3:
                model.setEssayNum(model.getEssayNum() +1);
                break;
        }
        return model;
    }


}
